package com.example.dollycontrol;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffColorFilter;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private final int LAUNCH_DELAY = 0;
	private final int TIMER_PERIOD = 100; //in milliseconds
	
	private DollyTimer dollyTimer;
	private EasingFunction easingFunction;
	private DollySlider slider;
	private TextView sliderOutputText;
	private EditText durationInput;
	private Button launchButton;
	
	private Handler handlerUI;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        
        handlerUI = new Handler();
        
        setContentView(R.layout.activity_main);
        
        slider = (DollySlider) findViewById(R.id.dollyslider);
        slider.registerForChangedNotification(this);
        sliderOutputText = (TextView) findViewById(R.id.output);
        
        easingFunction = (EasingFunction) findViewById(R.id.easingfunction);
        easingFunction.setSliderReference(slider);
        
        launchButton = (Button) findViewById(R.id.button1);
        durationInput = (EditText) findViewById(R.id.lapseTime);
        
        dollyTimer = new DollyTimer(slider, handlerUI);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void onLauncherClicked(View view){
    	if(slider.isTimeLapseInProgress()){
    		resetSlider();
    	}
    	else{
    		String timeInput = durationInput.getText().toString();
    		if(timeInput.length() > 0 && isParsableToInt(timeInput)){
        		slider.setTimeLapseInProgress(true);
        		slider.setTimeLapseDuration(Integer.parseInt(timeInput)*1000);
        		launchButton.setText("Abort");
        		launchButton.getBackground().setColorFilter(new PorterDuffColorFilter(Color.RED, Mode.MULTIPLY));
            	dollyTimer.launchTimer(LAUNCH_DELAY, TIMER_PERIOD);
    		}
    		else{
    			showPopUpMessage("Please enter a number!");
    		}
    	}
    }
    
    /*
     * rests the slider bar 
     */
    
    public void resetSlider(){
    	dollyTimer.getTimer().cancel();
    	slider.setMinHandlePosition(slider.INIT_MIN);
    	slider.setMaxHandlePosition(slider.INIT_MAX);
    	slider.setDollyTravelLength(-1);
		slider.setTimeLapseInProgress(false);
		slider.resetMarkers();
    	launchButton.setText("Launch");
    	launchButton.getBackground().setColorFilter(new PorterDuffColorFilter(Color.WHITE, Mode.MULTIPLY));
    	slider.invalidate();
    }
    
    /*
     * is called by the slider when a value has changed
     */
    
    public void sliderHasChangedNotification(Notification eventType){
    	if(eventType.equals(Notification.DRAG_EVENT)){
    		sliderOutputText.setText("Slide from "+ (slider.RAIL_LENGTH * (int)slider.getMinHandle()) / 100 + " mm to " + (slider.RAIL_LENGTH * (int)slider.getMaxHandle()) / 100 + " mm");
    	}
    	easingFunction.sliderHasChangedNotification(eventType);
    }
    
    /*
     * displays a string as a toast pop up in the lower right corner 
     */
    
    public void showPopUpMessage(String message){
    	Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
    	toast.setGravity(Gravity.BOTTOM|Gravity.RIGHT, 0, 100);
    	toast.show();
    }
    
    /*
     * returns true if a string is an integer, false otherwise
     */
    
    public boolean isParsableToInt(String i){
	    try{
	    	Integer.parseInt(i);
	    	return true;
	    }
	    catch(NumberFormatException nfe){
	    	return false;
	    }
    }
}
